import { ElNotification } from 'element-plus'
import { Destroyable, react, reactJson, Event, track, bind } from "xbsj-xe2/dist-node/xe2-base-utils";
import { ESAlarm } from 'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main';
export class AlarmCollection extends Destroyable {
    // 报警集合
    _map = new Map();

    // 开关
    _switch = this.disposeVar(react(false));
    get switch() { return this._switch.value; }
    set switch(value) { this._switch.value = value }
    get switchChanged() { return this._switch.changed; }

    // 全局显隐控制(EarthSDK响应式属性)
    _show = this.disposeVar(react(false));
    get show() { return this._show.value; }
    set show(value) { this._show.value = value }
    get showChanged() { return this._show.changed; }

    // 数据更新事件
    _updateEvent = this.disposeVar(new Event())
    get updateEvent() { return this._updateEvent }
    setData(data) {
        if (data) {
            this.create(data)
        } else {
            this.update(data)
        }
    }
    constructor(_myProjectManager) {
        super();
        this._myProjectManager = _myProjectManager
        setInterval(() => {
            var time = Date.now()
            this._map.forEach(e => {
                const diff = Math.floor((time - e.createTime) / 1000)
                if (diff > 60) {
                    this._myProjectManager.destroySceneObject(e)
                    this._map.delete(e.extras.id)
                    e = undefined
                    this.updateEvent.emit()
                }
            })
        }, 5000)

        // 全局控制显隐属性，监听变化
        this.showChanged.don((val) => {
            this._map.forEach(item => {
                item.show = val
            })
        })
    }

    create(item) {
        const { id, name, position } = item
        const sceneObj = this._myProjectManager.createSceneObject(ESAlarm)
        if (!sceneObj) return
        sceneObj.position = position
        sceneObj.name = name
        sceneObj.createTime = Date.now()
        sceneObj.extras = item
        sceneObj.show = this.show
        sceneObj.isFlyInCreated = this.show
        this._map.set(id, sceneObj)
        this.updateEvent.emit()
    }
    update(item) {
        const { id, name, position } = item
        const sceneObj = this._map.get(id)
        if (!sceneObj) return
        sceneObj.position = position
        sceneObj.name = name
        sceneObj.extras = item
        sceneObj.show = this.show
        sceneObj.createTime = Date.now()
        this.updateEvent.emit()
    }

}